package cn.edu.hznu.spring;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import org.xml.sax.SAXException;

public class SpringByXmlAndAnnotation {
	private Map<String, Object> beanId2Class = new HashMap<String, Object>();
	
	private SpringByXml springXml = new SpringByXml();
	private SpringByAnnotation springAnnotation = new SpringByAnnotation();
	
	public void beanFactory() {
		try {
			springXml.load();
			springAnnotation.load();
		} catch (IOException | SAXException | URISyntaxException e) {
			e.printStackTrace();
		}
	}
	
	public Object getBean(String beanId) {
		Object obj = beanId2Class.get(beanId);
		if (null != obj) {
			return obj;
		}
		try {
			obj = springXml.getBean(beanId);
			if (obj == null) {
				obj = springAnnotation.getBean(beanId);
			}
			if (obj != null) {
				beanId2Class.put(beanId, obj);
			}
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return obj;
	}
	
	
	
	
}
